package com.his.dictresource.controller;

import com.his.config.annotation.CurrentUser;
import com.his.dictresource.service.DictResourceItemService;
import com.his.dictresource.service.DictResourceService;
import com.his.dictresource.service.ResourceControlService;
import com.his.dictresource.vo.DictResourceItemVo;
import com.his.dictresource.vo.DictResourceVo;
import com.his.dictresource.vo.ResourceControlVo;
import com.his.login.entity.DictEmployee;
import com.xiao.common.controller.ResultBaseController;
import com.xiao.common.entity.BaseEntity;
import com.xiao.common.util.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author jzy
 * @Date 2020/3/16 15:28
 * @Version 1.0
 */
@RestController
@RequestMapping(value="/api/resource")
public class DictResourceController extends ResultBaseController {

    private  final Logger logger = (Logger) LoggerFactory.getLogger(this.getClass());

    @Resource
    private DictResourceService dictResourceService;

    @Resource
    private DictResourceItemService dictResourceItemService;

    @Resource
    private ResourceControlService resourceControlService;

    /********************************************资源管理定义表(排班)**************************************************/

    @PostMapping(value="/resource_list.do")
    public ResultUtil resource_list(@CurrentUser DictEmployee employee, @RequestBody DictResourceVo dictResourceVo){
        try {
            return this.success("成功", dictResourceService.selectDictResourceList(employee,dictResourceVo));
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }

    @PostMapping(value="/resource_add.do")
    public ResultUtil resource_add(@Validated @RequestBody DictResourceVo dictResourceVo){
        try {
            dictResourceService.insertDictResource(dictResourceVo);
            return this.success("成功", null);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }

    @PostMapping(value="/resource_del.do")
    public ResultUtil resource_del( @Validated @RequestBody String resourceTypeId,String resourceId,String dateTypeId){
        try {
            dictResourceService.deleteDictResource(resourceTypeId,resourceId,dateTypeId);
            return this.success("成功", null);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }

/********************************************资源费用定义表**************************************************/
    /**
     * @param dictEmployee
     * @param dictResourceVo
     *  查询挂号收费项目
     * @return com.xiao.common.util.ResultUtil
     * @author jzy
     * @creed: Talk is cheap,show me the code
     * @date 2020/3/27 21:51
     */
    @PostMapping(value="/reitem_list.do")
    public ResultUtil reItem_list(@CurrentUser DictEmployee dictEmployee,@Validated @RequestBody DictResourceVo dictResourceVo){
        try {
            return this.success("成功", dictResourceItemService.selectDictResourceItemList(dictEmployee,dictResourceVo));
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }

    @PostMapping(value="/reitem_add.do")
    public ResultUtil reItem_add(@CurrentUser DictEmployee dictEmployee, @Validated @RequestBody DictResourceItemVo dictResourceItemVo ){
        try {
            dictResourceItemService.deleteDictResourceItem(dictResourceItemVo.getItemid(),dictResourceItemVo.getResourcetypeid());
            dictResourceItemService.insertDictResourceItem(dictResourceItemVo);
            return this.success("成功", null);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }

    @PostMapping(value="/reitem_del.do")
    public ResultUtil reItem_del(@CurrentUser DictEmployee dictEmployee, @RequestBody String itemId,String resourceTypeId){
        try {
            dictResourceItemService.deleteDictResourceItem(itemId,resourceTypeId);
            return this.success("成功", null);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }

    /********************************************资源管理控制表**************************************************/

    @PostMapping(value="/recontrol.do")
    public ResultUtil reControl(@RequestBody ResourceControlVo resourceControlVo,@CurrentUser DictEmployee dictEmployee){
        try {
            return this.success("成功", resourceControlService.selectResourceControl(resourceControlVo));
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }

    @PostMapping(value="/control_datetypeid.do")
    public ResultUtil control_datetypeid(@CurrentUser DictEmployee dictEmployee){
        try {
            return this.success("查询日期类型成功", resourceControlService.selectDaytypeid(dictEmployee));
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }

    /** 
     * 
     * 查询号源剩余数量
     * [employee] 
     * @return com.xiao.common.util.ResultUtil
     * @date 2020/3/28 9:49
     */
    @PostMapping(value="/risdueamount.do")
    public ResultUtil risdueamount(@CurrentUser DictEmployee employee){
        try {
            return this.success("成功", resourceControlService.selectRosourceControlRisdueamount(employee));
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }
}
